<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="w-full max-w-xl" [formGroup]="argumentFormGroup">
  <div class="tb-form-panel no-border no-padding mb-2">
    <div class="tb-form-panel-title">{{ 'calculated-fields.argument-settings' | translate }}</div>
    <div class="tb-form-panel no-border no-padding">
      <div class="tb-form-row">
        <div class="fixed-title-width tb-required">{{ 'calculated-fields.argument-name' | translate }}</div>
        <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
          <input matInput autocomplete="off" name="value" formControlName="argumentName" maxlength="255" placeholder="{{ 'action.set' | translate }}"/>
          @if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('required')) {
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                      [matTooltip]="'calculated-fields.hint.argument-name-required' | translate"
                      class="tb-error">
              warning
            </mat-icon>
          } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('duplicateName')) {
          <mat-icon matSuffix
                    matTooltipPosition="above"
                    matTooltipClass="tb-error-tooltip"
                    [matTooltip]="'calculated-fields.hint.argument-name-duplicate' | translate"
                    class="tb-error">
            warning
          </mat-icon>
        } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('pattern')) {
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                      [matTooltip]="'calculated-fields.hint.argument-name-pattern' | translate"
                      class="tb-error">
              warning
            </mat-icon>
          } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('maxlength')) {
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                      [matTooltip]="'calculated-fields.hint.argument-name-max-length' | translate"
                      class="tb-error">
              warning
            </mat-icon>
          } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('forbiddenName')) {
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                      [matTooltip]="'calculated-fields.hint.argument-name-forbidden' | translate"
                      class="tb-error">
              warning
            </mat-icon>
          }
        </mat-form-field>
      </div>
      <ng-container [formGroup]="refEntityIdFormGroup">
        <div class="tb-form-row">
          <div class="fixed-title-width">{{ 'entity.entity-type' | translate }}</div>
          <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="entityType">
              @for (type of argumentEntityTypes; track type) {
                <mat-option [value]="type">{{ ArgumentEntityTypeTranslations.get(type) | translate }}</mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        @if (ArgumentEntityTypeParamsMap.has(entityType)) {
          <div class="tb-form-row">
            <div class="fixed-title-width tb-required">{{ ArgumentEntityTypeParamsMap.get(entityType).title | translate }}</div>
            <tb-entity-autocomplete
              class="flex flex-1"
              #entityAutocomplete
              formControlName="id"
              inlineField
              [placeholder]="'action.set' | translate"
              [required]="true"
              [entityType]="ArgumentEntityTypeParamsMap.get(entityType).entityType"
              (entityChanged)="entityNameSubject.next($event?.name)"
            />
          </div>
        }
      </ng-container>
      <ng-container [formGroup]="refEntityKeyFormGroup">
        <div class="tb-form-row">
          <div class="fixed-title-width tb-required">{{ 'calculated-fields.argument-type' | translate }}</div>
          <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="type">
              @for (type of argumentTypes; track type) {
                <mat-option [value]="type">{{ ArgumentTypeTranslations.get(type) | translate }}</mat-option>
              }
            </mat-select>
            @if (refEntityKeyFormGroup.get('type').hasError('required') && refEntityKeyFormGroup.get('type').touched) {
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="'calculated-fields.hint.argument-type-required' | translate"
                        class="tb-error">
                warning
              </mat-icon>
            }
          </mat-form-field>
        </div>
        @if (entityFilter.singleEntity?.id || entityType === ArgumentEntityType.Current || entityType === ArgumentEntityType.Tenant) {
          @if (refEntityKeyFormGroup.get('type').value !== ArgumentType.Attribute) {
            <div class="tb-form-row">
              <div class="fixed-title-width tb-required">{{ 'calculated-fields.timeseries-key' | translate }}</div>
              @if (refEntityKeyFormGroup.get('type').value === ArgumentType.LatestTelemetry) {
                <ng-container [ngTemplateOutlet]="timeseriesKeyAutocomplete"/>
              } @else {
                <ng-container [ngTemplateOutlet]="timeseriesKeyAutocomplete"/>
              }
              <ng-template #timeseriesKeyAutocomplete>
                <tb-entity-key-autocomplete class="flex-1" formControlName="key" [dataKeyType]="DataKeyType.timeseries" [entityFilter]="entityFilter"/>
              </ng-template>
            </div>
          } @else {
            @if (enableAttributeScopeSelection) {
              <div class="tb-form-row">
                <div class="fixed-title-width tb-required">{{ 'calculated-fields.attribute-scope' | translate }}</div>
                <mat-form-field appearance="outline" subscriptSizing="dynamic" class="flex-1">
                  <mat-select formControlName="scope">
                    <mat-option [value]="AttributeScope.SERVER_SCOPE">
                      {{ 'calculated-fields.server-attributes' | translate }}
                    </mat-option>
                    <mat-option [value]="AttributeScope.CLIENT_SCOPE">
                      {{ 'calculated-fields.client-attributes' | translate }}
                    </mat-option>
                    <mat-option [value]="AttributeScope.SHARED_SCOPE">
                      {{ 'calculated-fields.shared-attributes' | translate }}
                    </mat-option>
                  </mat-select>
                </mat-form-field>
              </div>
            }
            <div class="tb-form-row">
              <div class="fixed-title-width tb-required">{{ 'calculated-fields.attribute-key' | translate }}</div>
              <tb-entity-key-autocomplete
                formControlName="key"
                class="flex-1"
                [dataKeyType]="DataKeyType.attribute"
                [entityFilter]="entityFilter"
                [keyScopeType]="argumentFormGroup.get('refEntityKey').get('scope').value"
              />
            </div>
          }
        }
      </ng-container>
      @if (refEntityKeyFormGroup.get('type').value !== ArgumentType.Rolling) {
        <div class="tb-form-row">
          <div class="fixed-title-width">{{ 'calculated-fields.default-value' | translate }}</div>
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
            <input matInput autocomplete="off" name="value" formControlName="defaultValue" placeholder="{{ 'action.set' | translate }}"/>
          </mat-form-field>
        </div>
      } @else {
        <div class="tb-form-row">
          <div class="fixed-title-width tb-required">{{ 'calculated-fields.time-window' | translate }}</div>
          <tb-timeinterval
            subscriptSizing="dynamic"
            appearance="outline"
            class="time-interval-field flex-1"
            formControlName="timeWindow"
          />
        </div>
        @if (maxDataPointsPerRollingArg) {
          <div class="tb-form-row limit-field-row">
            <div class="fixed-title-width tb-required">{{ 'calculated-fields.limit' | translate }}</div>
            <div class="limit-slider-container flex w-full flex-1 flex-row items-center justify-start">
              <mat-slider class="flex-1" min="1" max="{{maxDataPointsPerRollingArg}}">
                <input matSliderThumb formControlName="limit" [value]="argumentFormGroup.get('limit').value"/>
              </mat-slider>
              <mat-form-field class="limit-slider-value" subscriptSizing="dynamic" appearance="outline">
                <input matInput formControlName="limit" type="number" step="1" [value]="argumentFormGroup.get('limit').value" min="1" max="{{maxDataPointsPerRollingArg}}"/>
              </mat-form-field>
            </div>
          </div>
        }
      }
    </div>
  </div>
  <div class="flex justify-end gap-2">
    <button mat-button
            color="primary"
            type="button"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button
            color="primary"
            type="button"
            (click)="saveArgument()"
            [disabled]="argumentFormGroup.invalid || !argumentFormGroup.dirty">
      {{ buttonTitle | translate }}
    </button>
  </div>
</div>
